//
//  CityViewController.m
//  NiuBanJin
//
//  Created by Billy on 15/11/12.
//  Copyright © 2015年 zzjr. All rights reserved.
//

#import "CityViewController.h"
#import "City.h"
#import "CountryViewController.h"

@interface CityViewController ()<UITableViewDataSource,UITableViewDelegate>

@property (nonatomic, strong) UITableView *tableView; //数据源显示
@property (nonatomic, strong) NSMutableArray *mutableArray_allCities;//所有市

@end

@implementation CityViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self.view addSubview:self.tableView];
     [self.navigationItem setTitle:@"选择市"];
}

- (void)viewWillAppear:(BOOL)animated {
    
    [super viewWillAppear:animated];
    
    [self getDataFromServer];
}

#pragma mark - data preparation

- (void) getDataFromServer {
    
    NSDictionary *dic_params = @{@"userId":GETCURRENTUSERINOBYPRAMETERES(user_userId),@"token":GETCURRENTUSERINOBYPRAMETERES(user_userToken),@"provinceId":self.str_id};
    
    [[NetworkUtil sharedNetworkUtil] zzjrTrade_get_cityByParmeteres:dic_params transferResultUsingBlock:^(id parsedData, NSError *error) {
        
        if (!error) {
            
            [self.mutableArray_allCities removeAllObjects]; //防止重复
            
            NSDictionary *dic = parsedData;
            
            if ([dic[@"status"] integerValue] == 0) {
            
                for (NSDictionary *dic_element in dic[@"result"]) {
                    
                    City *tmp = [[City alloc] initWithAttributes:dic_element];
                    [self.mutableArray_allCities addObject:tmp];
                }
            }
            
            if ([self.mutableArray_allCities count] > 0) {
                
                [self.tableView reloadData];
            }
            
        }else {
            
            [UsefulUtil showAlertView:error.description];
        }
    }];
}

#pragma mark - TableView datasource && delegate

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    
    return 1;
}

- (NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    return [self.mutableArray_allCities count];
}

- (UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    static NSString *identifer = @"cellCity" ;
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifer];
    
    if (!cell) {
        
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:identifer];
    }
    
    City *tmp = [self.mutableArray_allCities objectAtIndex:indexPath.row];
    
    [cell.textLabel setText:tmp.city_name];
    return  cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    City *tmp = [self.mutableArray_allCities objectAtIndex:indexPath.row];
    
    [UsefulUtil saveCustomObject:tmp intoUserDefaultByKey:@"city"];
    
    CountryViewController *countryVC = [[CountryViewController alloc] init];
    
    [countryVC setStr_id:tmp.city_id];
    
    [self.navigationController pushViewController:countryVC animated:YES];
    
}

#pragma mark - setter method

- (NSMutableArray *)mutableArray_allCities {
    
    if (!_mutableArray_allCities) {
        
        _mutableArray_allCities = [NSMutableArray array];
    }
    return _mutableArray_allCities;
}

- (UITableView *)tableView {
    
    if (!_tableView) {
        
        _tableView = [[UITableView alloc] initWithFrame:self.view.frame style:UITableViewStylePlain];
        [_tableView setDelegate:self];
        [_tableView setDataSource:self];
        [_tableView setAutoresizingMask:UIViewAutoresizingFlexibleHeight];
    }
    
    return _tableView;
}

@end
